package cn.ffcs.uec.outStaff.component;

import java.util.HashMap;
import java.util.Map;

import lombok.Getter;
import lombok.Setter;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutStaffTrialListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecTrialManager;
import cn.ffcs.uec.staff.model.UecTrial;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;

/**
 * @Title:人员试用情况 CRUD插件
 * @Description:
 * @Copyright: FFCS（C） 2017
 * @Company: FFCS
 * @Version 0.0.1 date:2018年01月10日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecOutStaffTrialListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 外包人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	private UecOutStaffTrialListboxBean bean = new UecOutStaffTrialListboxBean();
	/**
	 * 人员试用情况实体
	 */
	private UecTrial uecTrial;

	private UecTrialManager uecTrialManager = (UecTrialManager) ApplicationContextUtil.getBean("uecTrialManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOutStaffTrialListboxExt() {
		Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_TRIAL_EXT_LISTBOX_ZUL, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRIAL_QUERY, this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_TRIAL_QUERY_RESPONSE);

		this.addForward(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TRIAL, this,
				UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TRIAL_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		this.setButtonValid(false, false, false, false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecOutStaffTrialList() throws Exception {
		if (this.bean.getUecOutStaffTrialListbox().getSelectedCount() > 0) {
			uecTrial = (UecTrial) bean.getUecOutStaffTrialListbox().getSelectedItem().getValue();
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffTrialQueryResponse(ForwardEvent event) {
		uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if (null != uecOutStaffInfo) {
			if (null != uecOutStaffInfo.getOutStaffId()) {
	            //设置操作按钮，非盟市主管不可操作按钮
	            if(PermissionUtil.isNoMainOutStaff()) {
	                this.setButtonValid(false, false, false, false);
	            }else {
	                this.setButtonValid(true, true, true, false);
	            }
	            uecTrial = new UecTrial();
                uecTrial.setStaffId(uecOutStaffInfo.getOutStaffId());
			}
			this.onQueryUecOutStaffTrial();
		}
	}

	public void onCleanUecOutStaffTrialResponse(ForwardEvent event) {
		clearVariable();
		uecOutStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecOutStaffTrialListbox());
		this.bean.getUecOutStaffTrialListboxPaging().setTotalSize(1);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canView, boolean canAdd, boolean canEdit, boolean candel) {
		this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecOutStaffTrial() {
		this.bean.getUecOutStaffTrialListboxPaging().setActivePage(0);
		this.onQueryUecOutStaffTrialList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecOutStaffTrial() {
		ListboxUtils.clearListbox(this.bean.getUecOutStaffTrialListbox());
		this.bean.getUecOutStaffTrialListboxPaging().setTotalSize(1);
	}

	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecOutStaffTrial() throws Exception, InterruptedException {
		if (!StrUtil.isNullOrEmpty(uecTrial) && !StrUtil.isNullOrEmpty(uecTrial.getId())) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecTrial", uecTrial);
			Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_TRIAL_EDIT_ZUL,
					this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要查看的记录", "提示信息");
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception @throws
	 */
	public void onAddUecOutStaffTrial() throws Exception {
		if (null != uecOutStaffInfo) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "add");
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_TRIAL_EDIT_ZUL,
					this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecTrial = (UecTrial) event.getData();
						onQueryUecOutStaffTrialList();
					}
				}
			});
		}
	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecOutStaffTrial() throws Exception, InterruptedException {
		if (null != uecTrial.getId()) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "edit");
			map.put("uecTrial", uecTrial);
			Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_TRIAL_EDIT_ZUL,
					this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecTrial = (UecTrial) event.getData();
						onQueryUecOutStaffTrialList();
					}
				}
			});
		} else {
			ZkUtil.showError("请选择要编辑的记录", "提示信息");
			return;
		}
	}

	/**
	 * 删除按钮
	 */
	public void onDelUecOutStaffTrial() {
		if (uecTrial.getId() != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecTrial.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecOutStaffTrialListbox(), uecTrial, "del");
						uecTrial = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选择要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecOutStaffTrialListboxPaging() {
		this.onQueryUecOutStaffTrialList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecTrial = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecOutStaffTrialList() {
		if (null != uecTrial) {
			PageInfo pageInfo = uecTrialManager.queryPageInfoByUecTrial(uecTrial,
					this.bean.getUecOutStaffTrialListboxPaging().getActivePage() + 1,
					this.bean.getUecOutStaffTrialListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
			this.bean.getUecOutStaffTrialListbox().setModel(list);
			this.bean.getUecOutStaffTrialListboxPaging().setTotalSize(pageInfo.getTotalCount());
		}
	}

}
